import { defineStore } from 'pinia'
import { createCommonGetters, createCommonActions } from "../helper"
import $api from '@/api'


export const useSubjectStore = defineStore('subject', {
  state: () => ({
    loading: false,
    params: {},
    data: [],
    currentItem: {}
  }),
  getters: {
    ...createCommonGetters
  },
  actions: {
    ...createCommonActions(),
    // 获取题目列表数据
    async fetchSubjectList() {
      try {
        if (this.isArrayEmpty("data")) {
          const res = await $api.dict.getAllSubject()
          if (res.code === 200) {
            this.data = res.data
            this.currentItem = res.data[0] || {}
          } else {
            $modal.message({ message: '【$api.dict.getAllSubject】接口报错，请联系管理员！', type: 'error' })
          }
        }
      }
      catch (error) {
        console.error('fetchSubjectList:', error)
      }

    },
    //重置
    reset() {
      this.$reset()
    },
  },
  persist: {
    enabled: true,
    storage: localStorage
  }
})
